#include "head.h"
/* Usage: */

int work(client_status* user, status_t* status){

    //while(1){
        printf("进入work\n");
        sendMsg *msg = (sendMsg*)calloc(1, sizeof(sendMsg));
        printf("!!!!!!!!!!!!!!!!!!!!!!!!\n");
        int res_recv = recv(user->net_fd, msg, sizeof(*msg), MSG_WAITALL);
        printf("msg : %s\n", msg->msg);
        if(res_recv==0 || res_recv==-1){
            printf("用户端关闭\n");
            //unlogin(user, user->net_fd);
            user->islogin = false;
            return 0;
        }
        //printf("msg : %s\n", msg.msg);
        if(msg->msg[0]!='/' && strcmp(msg->msg, "..")){
            char temp[4096] = {0};
            snprintf(temp, sizeof(temp), "/%s", msg->msg);
            strcpy(msg->msg, temp);
        }
        user->msg = msg;
        int mode = user->msg->mode;
        printf("mode = %d, id = %s\n", user->msg->mode, user->user_id);
        
        if(mode==1){
            my_cd(*msg, user, user->net_fd);
            free(msg);
        } else if(mode==2){
            my_ls(*msg, user, user->net_fd);
            free(msg);
        } else if(mode==5){
            my_rm(*msg, user, user->net_fd);
            free(msg);
        } else if(mode==6){
            //my_pwd(msg, user, user->net_fd);
        } else if(mode==7){
            my_mkdir(*msg, user, user->net_fd);
            free(msg);
        } else if(mode==3 || mode==4){
            
            pthread_mutex_lock(&status->lock);
            bool ret_push = push(status->commend_q, user);
            if(!ret_push){
                printf("!!!长命令入队失败！\n");
                return -1;
            }
            printf("已入队！\n");
            printf("size = %d\n", status->commend_q->size);
            pthread_cond_signal(&status->cond);
            pthread_mutex_unlock(&status->lock);
        } else if(mode==-1){
            //unlogin(user, user->net_fd);
            user->islogin = false;
            return 0;
        }
    //}
    return 0; 
}
